package com.riaevolution.components
{
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.geom.Rectangle;
	import flash.printing.PrintJob;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.core.UIComponent;
	
	public class PrintHelper
	{
		
		public function PrintHelper(collection:*) {
			var i:int = collection.length-1;
			while(i>=0) {
				this.setupComponent(collection[i]);
				i--;
			}
		}
		
		private function setupComponent(component:UIComponent):void { 
			var cm:ContextMenu = new ContextMenu();
			cm.hideBuiltInItems();
			var cmi:ContextMenuItem = new ContextMenuItem('Print '+component.className,true);
			cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,handleSelectPrint);
			cm.customItems.push(cmi);
			component.contextMenu = cm;
		}
		private function handleSelectPrint(event:ContextMenuEvent):void {
			handlePrint(event.contextMenuOwner as UIComponent);
		}
		private function handlePrint(component:UIComponent):void {
			var pj:PrintJob = new PrintJob();
			if(pj.start()) {
				var rec:Rectangle = new Rectangle(0,0,component.width, component.height);
				pj.addPage(component as Sprite,rec);
				pj.send();
			}
		}
	}
}